// File: src/transaction_bbs.rs
use serde::{Serialize, Deserialize};
use crate::{
    bbs::post::BbsPost,
    // wallet::Wallet,
};
use crate::bbs::comment::BbsComment;  // 添加引用
use crate::transaction::Transaction;
//use crate::transaction::TransactionType;

/*
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BbsTransaction {
    pub post_hash: String,
    pub gitee_url: String,
    pub author: String,
    pub signature: String,
}*/

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BbsTransaction {
    Post(BbsPost),
    Comment(BbsComment),
}

#[allow(dead_code)] // 如果后续需要哈希功能
impl BbsTransaction {
//    pub fn hash(&self) -> Vec<u8> {
    pub fn hash(&self) -> String {
        match self {
//            Self::Post(p) => p.content_hash.clone(),
//            Self::Comment(c) => c.content_hash.clone(),
            Self::Post(p) => hex::encode(&p.content_hash),
            Self::Comment(c) => hex::encode(&c.content_hash),
        }
    }

    pub fn new_comment(comment: &BbsComment) -> Self {
        Self::Comment(comment.clone())
    }
}

/*
impl BbsTransaction {
    pub fn new(post: &BbsPost, wallet: &Wallet, gitee_url: &str) -> Self {
        let content_hash = calculate_content_hash(&post.content);
        let signature = wallet.sign(&content_hash);
        
        Self {
            post_hash: content_hash,
            gitee_url: gitee_url.to_string(),
            author: wallet.public_key(),
            signature,
        }
    }
}
*/

/* 步骤 3：删除冗余哈希函数
fn calculate_content_hash(content: &str) -> String {
    use sha2::{Sha256, Digest};
    let mut hasher = Sha256::new();
    hasher.update(content);
    format!("{:x}", hasher.finalize())
}*/

// src/transaction_bbs.rs
impl From<BbsTransaction> for Transaction {
    fn from(bbs_tx: BbsTransaction) -> Self {
/*        let (tx_type, sender, data) = match bbs_tx {
            BbsTransaction::Post(p) => (
                TransactionType::BbsPost,
                p.author,
                p.content_hash
            ),
            BbsTransaction::Comment(c) => (
                TransactionType::BbsComment,
                c.author,
                c.content_hash
            ),
        };

        // 获取系统签名
        // let signature = get_system_signature(&data);

        Transaction {
            tx_type,
            sender,
            receiver: "BBS_SYSTEM".to_string(),
            amount: 0,
            //signature: vec![],  // 暂不实现签名,
	    signature: Vec::new(),  // 暂时留空
            hash: hex::encode(data)
        }
*/
        match bbs_tx {
/*            BbsTransaction::Post(p) => Transaction::BbsPost {
                author: p.author,
                content_hash: p.content_hash,
                proof: String::new(),
                signature: Vec::new(),
                hash: p.content_hash,
            },
            BbsTransaction::Comment(c) => Transaction::BbsComment {
                author: c.author,
                content_hash: c.content_hash,
                post_hash: c.post_hash,
                signature: Vec::new(),
                hash: c.content_hash,
            },
*/
/*            BbsTransaction::Post(p) => {
                // 将 Vec<u8> 转换为 hex 字符串
                let content_hash = hex::encode(p.content_hash);
                Transaction::BbsPost {
                    author: p.author,
                    content_hash,
                    proof: String::new(),
                    signature: Vec::new(),
                    hash: content_hash.clone(),
                }
            },
*/
            BbsTransaction::Post(p) => {
//                let content_hash = hex::encode(p.content_hash); // 转换Vec<u8>为String
		let content_hash = hex::encode(p.content_hash.clone()); // ✅ 克隆哈希值
                Transaction::BbsPost {
                    author: p.author,
                    // content_hash,
		    content_hash: content_hash.clone(),
                    //proof: String::new(),
		    proof: p.url, // ✅ 确保URL存入proof字段
                    signature: Vec::new(),
                    hash: content_hash.clone(),
                }
            }
            BbsTransaction::Comment(c) => {
                let content_hash = hex::encode(c.content_hash);
                Transaction::BbsComment {
                    post_hash: c.post_hash,
                    author: c.author,
//                    content_hash,
		    content_hash: content_hash.clone(), // ✅ 明确克隆
		    proof: c.url, // 新增url字段
                    signature: Vec::new(),
                    hash: content_hash.clone(),
                }
            }
        }
    }
}

// 系统签名函数示例
//fn get_system_signature(data: &[u8]) -> Vec<u8> {
//    let key_pair = load_system_key();
//    key_pair.sign(data).as_ref().to_vec()
//}
